<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "sys_menu".
 *
 * @property integer $id
 * @property string $titile
 * @property integer $show_order
 * @property integer $parent_id
 * @property string $module_name
 * @property string $controller_name
 * @property string $method_name
 * @property string $menu_url
 * @property integer $status
 * @property string $menu_icon
 * @property integer $display
 */
class SysMenu extends \yii\db\ActiveRecord
{
	/**
	 * 正常
	 * @var integer
	 */
	const NORMAL = 1;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sys_menu';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['titile'], 'required'],
            [['show_order', 'parent_id', 'status','display'], 'integer'],
            [['titile', 'module_name', 'controller_name', 'method_name', 'menu_url', 'menu_icon'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'titile' => '菜单标题',
            'show_order' => '显示次序',
            'parent_id' => '父ID',
            'module_name' => '模块名',
            'controller_name' => '控制器名',
            'method_name' => '方法名',
            'menu_url' => '菜单Url',
            'status' => '状态',
            'menu_icon' => '图标',
            'display' => '显示隐藏',
        ];
    }

    /**
     * 获取所有菜单
     * @param int $role_id 角色id
     * @return mixed
     */
    public function getAll($role_id)
    {
        if ($role_id < 1) {
            return [];
        }
//        $cache = Yii::$app->cache;
//        $cache->delete('sys_role_menu_' . $role_id);
//        if (($menuIdList = $cache->get('sys_role_menu_' . $role_id)) === false) {
            $menuList = SysRoleMenu::find()->where(['role_id' => $role_id])->asArray()->all();
            $menuIdList = [];
            foreach ($menuList as $val) {
                if (($role_id == 3 || $role_id == 4) && $val['menu_id'] == 51) {
                    continue;
                }
                $menuIdList[] = $val['menu_id'];
            }
//            $cache->set('sys_role_menu_' . $role_id, $menuIdList, 60);
//        }

        $list = self::find()->where(['display' => 1, 'id' => $menuIdList])->asArray()->orderBy('show_order')->all();
//        $list = self::find()->where(['status' => 1])->asArray()->orderBy('show_order')->all();

        return $this->list_to_tree_menu($list);
    }

    /**
     * 组合成Tree树
     * @param $list
     * @param int $pid
     * @return array
     */
    public function list_to_tree_menu($list, $pid = 0)
    {
        // 创建Tree
        $tree = [];
        if (is_array($list)) {
            foreach ($list as $val) {
                if ($val['parent_id'] == $pid) {
                    $val['_child'] = $this->list_to_tree_menu($list, $val['id']);
                    $tree[] = $val;
                }
            }

        }

        return $tree;
    }

}
